/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.activity;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.activity.ActivityConfig;
import com.boxin.ims.modules.wechat.dao.activity.ActivityConfigDao;

/**
 * 业务活动模型Service
 * @author XiaoQian
 * @version 2014-04-10
 */
@Component
@Transactional(readOnly = true)
public class ActivityConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ActivityConfigService.class);
	
	@Autowired
	private ActivityConfigDao activityConfigDao;
	
	public ActivityConfig get(Long id) {
		return activityConfigDao.findOne(id);
	}
	
	public Page<ActivityConfig> find(Page<ActivityConfig> page, ActivityConfig activityConfig) {
		DetachedCriteria dc = activityConfigDao.createDetachedCriteria();
		if (activityConfig.getUser()!=null && activityConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", activityConfig.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(activityConfig.getName())){
			dc.add(Restrictions.like("name", "%"+activityConfig.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return activityConfigDao.find(page, dc);
	}
	
	
	public List<ActivityConfig> find(ActivityConfig activityConfig) {
		DetachedCriteria dc = activityConfigDao.createDetachedCriteria();
		if (activityConfig.getUser()!=null && activityConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", activityConfig.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(activityConfig.getName())){
			dc.add(Restrictions.like("name", "%"+activityConfig.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return activityConfigDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ActivityConfig activityConfig) {
		activityConfigDao.clear();
		activityConfigDao.save(activityConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		activityConfigDao.deleteById(id);
	}
	
}
